import json
from . import message_bp
import pymysql
from flask import jsonify
from flask import request
from apps.message.messageService import getMSByIDService, update_trailer_statusService, getTrailerReceiveMSByIDService, \
    getTrailerSendMSByIDService, getTrailerAllMessageByIDService


@message_bp.route('/message/hello', methods=["GET"])
def hello():
    return jsonify({"code": 200, "data": {"a": 1}})


@message_bp.route('/message/getMsByID', methods=["GET"])
def getMSByID():
    """
    根据信息ID查询信息的API
    返回一个名为data的list，里面包含发送双方ID以及内容
    ---
    tags:
      - 有关信息的API
    parameters:
      - name: message_ID
        in: GET
        type: number
        required: true
        description: 信息的ID号码
    responses:
      500:
        description: 并不存在此信息号
      200:
        description: 查询成功
        schema:
          id: awesome
          properties:
            data:
              type: array
              description: 查询信息为1返回的测试用例
              items:
                type: object
              default: [1, "2", 1, "3", "情况紧急，请求支援"]
    """
    print(1)
    return getMSByIDService(request.args.get("message_ID"))


@message_bp.route('/message/sendMessage', methods=["POST"])
def sendMessage():
    """
       发送信息的api
       ---
       tags:
         - 有关信息的API
       parameters:
         - name: sender
           in: POST
           type: string
           required: true
           description: 发送方ID
         - name: sender_type
           in: POST
           type: number
           required: true
           description: 发送方类型
         - name: receiver
           in: POST
           type: string
           required: true
           description: 接收方ID
         - name: message_content
           in: POST
           type: string
           required: true
           description: 信息具体内容

       responses:
         500:
           description: 信息发送失败
         200:
           description: 信息发送成功

    """
    data = json.loads(request.get_data())
    return update_trailer_statusService(data)


# 通过trailerID来查找trailer收到的所有message
@message_bp.route('/message/getTrailerReceiveMSByID', methods=["GET"])
def getTrailerReceiveMSByID():
    """
    根据信息ID查询信息的API
    返回一个名为data的list，里面包含发送双方ID以及内容
    ---
    tags:
      - 有关信息的API
    parameters:
      - name: message_ID
        in: GET
        type: number
        required: true
        description: 信息的ID号码
    responses:
      500:
        description: 并不存在此信息号
      200:
        description: 查询成功
        schema:
          id: awesome
          properties:
            data:
              type: array
              description: 查询信息为1返回的测试用例
              items:
                type: object
              default: [1, "2", 1, "3", "情况紧急，请求支援"]
    """

    return getTrailerReceiveMSByIDService(request.args.get("trailer_ID"))


# 通过trailerID来查找trailer收到的所有message
@message_bp.route('/message/getTrailerSendMSByID', methods=["GET"])
def getTrailerSendMSByID():
    """
    根据信息ID查询信息的API
    返回一个名为data的list，里面包含发送双方ID以及内容
    ---
    tags:
      - 有关信息的API
    parameters:
      - name: message_ID
        in: GET
        type: number
        required: true
        description: 信息的ID号码
    responses:
      500:
        description: 并不存在此信息号
      200:
        description: 查询成功
        schema:
          id: awesome
          properties:
            data:
              type: array
              description: 查询信息为1返回的测试用例
              items:
                type: object
              default: [1, "2", 1, "3", "情况紧急，请求支援"]
    """

    return getTrailerSendMSByIDService(request.args.get("trailer_ID"))

# 通过trailerID来查找trailer收到的所有message
@message_bp.route('/message/getTrailerAllMessageByID', methods=["GET"])
def getTrailerAllMessageByID():
    """
    根据信息ID查询信息的API
    返回一个名为data的list，里面包含发送双方ID以及内容
    ---
    tags:
      - 有关信息的API
    parameters:
      - name: message_ID
        in: GET
        type: number
        required: true
        description: 信息的ID号码
    responses:
      500:
        description: 并不存在此信息号
      200:
        description: 查询成功
        schema:
          id: awesome
          properties:
            data:
              type: array
              description: 查询信息为1返回的测试用例
              items:
                type: object
              default: [1, "2", 1, "3", "情况紧急，请求支援"]
    """

    return getTrailerAllMessageByIDService(request.args.get("trailer_ID"))
